import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../views/index.vue'
Vue.use(VueRouter)

const routes = [
	{
		path: '/',
		component: Index,
		children: [
			{
				path: '',
				redirect: '/home',
			},
			{
				path: '/home',
				component: () => import('../views/Home.vue'),
			},
			{
				path: '/order',
				component: () => import('../views/Order.vue'),
			},
			{
				path: '/me',
				component: () => import('../views/Me.vue'),
			},
			{
				path: '/address',
				component: () => import('../views/Address.vue'),
			},
			{
				path: '/city',
				component: () => import('../views/City.vue'),
			},
		],
	},
	{
		path: '/login',
		name: 'login',
		component: () => import('../views/Login.vue'),
	},
]

const router = new VueRouter({
	routes,
})
//路由守卫
router.beforeEach((to, from, next) => {
	const isLogin = localStorage.ele_login ? true : false
	if (to.path == '/login') {
		next()
	} else {
		//是否在登陆状态下
		isLogin ? next() : next('/login')
	}
})

export default router
